package cn.elwy.rcp.ui.explorer.editor;

import java.io.File;
import java.lang.reflect.InvocationTargetException;

import cn.elwy.rcp.core.model.ui.Condition;
import cn.elwy.rcp.core.model.ui.progress.ProgressMonitor;
import cn.elwy.rcp.ui.dialog.MsgDialog;
import cn.elwy.rcp.ui.editor.BaseEditorWizard;
import cn.elwy.rcp.ui.explorer.core.AppContext;
import cn.elwy.rcp.ui.explorer.core.model.Operation;
import cn.elwy.rcp.ui.explorer.core.service.ProjectConfigService;
import cn.elwy.rcp.ui.explorer.core.vo.ProjectConfig;
import cn.elwy.rcp.ui.explorer.create.NewProjectModel;
import cn.elwy.rcp.ui.explorer.process.PackerProgress;

/**
 * 打包编辑器向导
 * @author huangsq
 * @version 1.0, 2014-09-11
 * @since 1.0, 2014-09-11
 */
public class PackerEditorWizard extends BaseEditorWizard {

	private File file;
	// private NewProjectModel newProjectModel;
	private ProjectConfig projectConfig;
	ProjectInfoEditorPage firstPage;
	ModuleChoiceEditorPage secoundPage;
	private ModuleChoiceModel moduleChoiceModel;

	public PackerEditorWizard(File file, Operation operation) {
		setWindowTitle("Editor Wizard");
		Condition condition = new Condition();
		condition.put(NewProjectModel.OPERATION, operation);
		this.file = file;
		condition.put(NewProjectModel.FILE_PATH, file);

		NewProjectModel newProjectModel = new NewProjectModel(condition);

		projectConfig = newProjectModel.getInput();
		moduleChoiceModel = new ModuleChoiceModel(projectConfig);

		setNeedsProgressMonitor(true);
	}

	public ModuleChoiceModel getModuleChoiceModel() {
		return moduleChoiceModel;
	}

	public void setModuleChoiceModel(ModuleChoiceModel moduleChoiceModel) {
		this.moduleChoiceModel = moduleChoiceModel;
	}

	@Override
	public void addPages() {
		firstPage = new ProjectInfoEditorPage();
		secoundPage = new ModuleChoiceEditorPage();
		addPage(firstPage);
		addPage(secoundPage);
	}

	@Override
	public boolean canFinish() {
		return super.canFinish() && this.secoundPage.getControl() != null;
	}

	@Override
	public boolean performFinish() {
		if (!super.performFinish() || !testLoginSvn()) {
			return false;
		}
		PackerProgress runnable = new PackerProgress(projectConfig);
		try {
			getContainer().run(true, true, runnable);
		} catch (InterruptedException e) {
			return false;
		} catch (InvocationTargetException e) {
			Throwable realException = e.getTargetException();
			MsgDialog.open(getShell(), realException);
			return false;
		}
		if (runnable.isError()) {
			MsgDialog.open(getShell(), runnable.getError());
			return false;
		} else {
			// String osString = Platform.getLocation().toOSString().replace("\\", "/");
			// File file = null;
			// file = new File(projectPath, ProjectConst.PACKER_FILE_NAME);
			// if (projectPath.equals(osString + "/" + projectName)) {
			// projectPath = osString;
			// file = new File(projectPath, projectName + "/" + ProjectConst.PACKER_FILE_NAME);
			// } else {
			// }
			// final IPath location = new Path(projectPath);
			ProjectConfigService service = AppContext.getService(ProjectConfigService.ID, ProjectConfigService.class);
			service.save(file, projectConfig);
			// projectConfig.setStartVersion(startVersion)

			MsgDialog.openInformation(getShell(), "打包成功！");
			return true;
		}
	}

	public boolean testLoginSvn() {
		boolean testLoginSvn = firstPage.testLoginSvn(new ProgressMonitor(null));
		if (!testLoginSvn) {
			getContainer().showPage(firstPage);
		}
		return testLoginSvn;
	}

	public ProjectConfig getProjectConfig() {
		return projectConfig;
	}

	public void setProjectConfig(ProjectConfig projectConfig) {
		this.projectConfig = projectConfig;
	}

}
